package com.eunion.boot.security;

import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import com.eunion.web.admin.entity.AdminUser;

/**
 * 用户实体.UserDetails代表了Spring Security的用户认证实体，带有用户名、密码、权限列表、过期特性等性质，可以自己声明类实现UserDetails接口
 * 
 * @since 2017年1月1日
 */
public class SysUserDetails extends AdminUser implements UserDetails {

	private static final long serialVersionUID = 6575789549397424669L;

	private List<String> auths;

	public SysUserDetails(AdminUser user, List<String> auths) {
		super(user.getUsername(), user.getPassword(), user.getStatus());
		this.auths = auths;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (auths == null || auths.size() < 1) {
			return AuthorityUtils.commaSeparatedStringToAuthorityList("");
		}
		StringBuilder commaBuilder = new StringBuilder();
		for (String auth : auths) {
			commaBuilder.append(auth).append(",");
		}
		String authorities = commaBuilder.substring(0, commaBuilder.length() - 1);
		return AuthorityUtils.commaSeparatedStringToAuthorityList(authorities);
	}

	@Override
	public String getPassword() {
		return super.getPassword();
	}

	@Override
	public String getUsername() {
		return super.getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}
